//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by FernFlower decompiler)
//

package ciicos.views.job;

import ciicos.base.UserInfo;
import ciicos.config.ConfigValueUtil;
import ciicos.exception.LoginException;
import ciicos.iview.service.IviewService;
import ciicos.service.LoginService;
import ciicos.util.StringUtil;
import ciicos.util.data.DataSourceContextHolder;
import ciicos.views.websocket.IviewWebSocketPoint;
import java.util.Map;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

@Component
@EnableScheduling
@EnableAsync
public class RealDataPointJob {
    private static final Logger log = LoggerFactory.getLogger(RealDataPointJob.class);
    @Autowired
    private IviewService iviewService;
    @Autowired
    protected ConfigValueUtil configValueUtil;
    @Autowired
    protected LoginService loginService;

    @Async
    @Scheduled(
        cron = "* * * * * ?"
    )
    public void RealDataJobGetValue() throws LoginException {
        if (IviewWebSocketPoint.getWebSocketMap().size() > 0) {
            log.debug("【定时推送】连接数：" + IviewWebSocketPoint.getWebSocketMap().size());

            for(Map.Entry<String, IviewWebSocketPoint> entry : IviewWebSocketPoint.getWebSocketMap().entrySet()) {
                String[] sessionIdAndType = ((String)entry.getKey()).split("@");
                String[] split = sessionIdAndType[1].split("--");
                String tenant = "";
                String names = "";
                if (split.length == 1) {
                    names = split[0];
                } else {
                    if (split.length != 2) {
                        log.debug("【定时推送】，参数格式不规范:");
                        return;
                    }

                    tenant = split[0];
                    names = split[1];
                }

                UserInfo userInfo = new UserInfo();
                if (StringUtil.isNotEmpty(tenant)) {
                    userInfo.setUserAccount(tenant + "@admin");
                } else {
                    userInfo.setUserAccount("admin");
                }

                this.switchTenant(userInfo);
                String realdata = this.iviewService.realdata((String)null, names, (Integer)null, (Integer)null);
                IviewWebSocketPoint.sendMessage(realdata, (String)entry.getKey());
            }
        }

    }

    protected UserInfo switchTenant(UserInfo userInfo) throws LoginException {
        String tenantId = "";
        String tenantDbConnectionString = "";
        Boolean isAssign = false;
        if (this.configValueUtil.isMultiTenancy()) {
            userInfo = this.loginService.getTenantAccount(userInfo);
            tenantId = userInfo.getTenantId();
            tenantDbConnectionString = userInfo.getTenantDbConnectionString();
            isAssign = userInfo.isAssignDataSource();
            DataSourceContextHolder.setDatasource(tenantId, tenantDbConnectionString, isAssign);
        }

        userInfo.setTenantId(tenantId);
        userInfo.setTenantDbConnectionString(tenantDbConnectionString);
        userInfo.setAssignDataSource(isAssign);
        return userInfo;
    }
}
